package com.zj.system.service.impl;

import com.seventh.common.pojo.Payload;
import com.seventh.common.pojo.R;
import com.seventh.common.utils.JwtUtils;

import com.zj.system.config.RsaKeyProperties;
import com.zj.system.mapper.MenuMapper;
import com.zj.system.mapper.RoleMapper;
import com.zj.system.pojo.entity.SysMenu;
import com.zj.system.pojo.entity.User;
import com.zj.system.service.MenuSer;
import com.zj.system.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: clms-seventh
 * @description: menu的逻辑控制层
 * @author: 张静
 * @create: 2021-07-18 20:10
 **/
@Service
public class MenuSerImpl implements MenuSer {
    @Resource
    private MenuMapper menuMapper;

    @Autowired
    private RsaKeyProperties rsaKeyProperties;
    @Override
    public R selectFunction() {
        HttpServletRequest request= WebUtil.getRequest();
        String token = request.getHeader("token");
        Payload<User> info = JwtUtils.getInfoFromToken(token, rsaKeyProperties.getPublicKey(), User.class);


        Integer userId = info.getUserInfo().getUserId();
        List<SysMenu> father= menuMapper.selectFuntion(userId);
        List<SysMenu> firstMenu=new ArrayList<>();
        for(SysMenu menu:father){
            Integer a=0;
            if (a.equals(menu.getParentId())){
                firstMenu.add(menu);
            }
        }

        for (SysMenu first:firstMenu){
            first.setSonList(findSon(father,first.getMenuId()));
        }
        return R.ok().put("father",firstMenu);
    }

    @Override
    public R fenFunction(Integer roleId) {

        List<SysMenu> father= menuMapper.selectMenuByRole(roleId);
        List<Integer> roleMenuIds=father.stream().map(item->{return item.getMenuId();}).collect(Collectors.toList());
        List<SysMenu> allList=menuMapper.selectList(null);
        List<SysMenu> firstMenu=new ArrayList<>();
        for(SysMenu menu:allList){
            Integer a=0;
            if (a.equals(menu.getParentId())){
                firstMenu.add(menu);
            }
        }
        for (SysMenu first:firstMenu){
            first.setSonList(findSon(allList,first.getMenuId()));
        }

        return R.ok().put("menus",firstMenu).put("roleMenuIds",roleMenuIds);
    }

    @Override
    @Transactional
    public R upMenu(Integer roleId, List<Integer> roleMenu) {
        menuMapper.deleteByRoleId(roleId);
        menuMapper.insertByRoleId(roleId, roleMenu);
        return R.ok();
    }



    private List<SysMenu> findSon(List<SysMenu> menus,Integer pid){

        List<SysMenu> children=new ArrayList<>();
        for(SysMenu child:menus){
            if (child.getParentId().equals(pid)){
                children.add(child);
            }
        }

        for (SysMenu c:children){

            c.setSonList(findSon(menus,c.getMenuId()));
        }
        return children;
    }
}
